import httpUtils from "../httpRequest";
import {
  AddressModel,
  AdsModel, ApplyTeamModel,
  BalanceModel,
  CategoryModel,
  CouponsModel,
  ExpressModel,
  FileModel,
  HomeTop, HotWordModel,
  InvoiceModel,
  LoginInfo,
  MachineModel, MoreOptions,
  NoticeModel,
  OrderItem,
  PageListModel,
  PayParamsModel,
  ProjectModel,
  RechargeModel,
  RechargeRecordModel,
  RelationModel,
  SPU,
  TeamModel,
  UniversityModel,
  UserinfoModel,
  ZiXunModel
} from "./model";
// eslint-disable-next-line import/first
import LocalData from "@/common/local_data";
import LocalUtils from "@/utils/local_utils";

// @ts-ignore
export async function getHomeData(): Promise<SPU[]> {
  // return await httpUtils.request("/weixin?method=readJsonTest&fileName=goods.json", "GET", {}, false, false, true);
  return [];
}

export async function getExpress(): Promise<ExpressModel[]> {
  // return await httpUtils.request("/weixin?method=readJsonTest&fileName=express.json", "GET", {}, false, false, true);
  return [];
}

// @ts-ignore
export async function getHomeTop(): Promise<HomeTop> {
  return await httpUtils.request("/weixin?method=readJsonTest&fileName=home.json", "GET", {}, false, false, true);
}


export async function getNoticeList(): Promise<NoticeModel[]> {
  // return await httpUtils.request("/weixin?method=readJsonTest&fileName=notice.json", "GET", {}, false, false, false);
  return [];
}


export async function loginApi(params: any): Promise<LoginInfo | null> {
  try {
    return await httpUtils.request("/base/login", "POST", params, false, false, true);
  } catch (e) {
    return null;
  }
}

export async function getPhoneApi(params: any): Promise<any> { // Changed from Promise<any | null>
  try {
    return await httpUtils.request("/base/phone", "GET", params, false, false, false);
  } catch (e) {
    return null;
  }
}

export async function updateUserApi(params: any): Promise<any> { // Changed from Promise<any | null>
  try {
    return await httpUtils.request("/user/update", "POST", params, false, false, false);
  } catch (e) {
    return null;
  }
}

///yckj/user/idCardVerification
export async function idCardVerification(params): Promise<any> { // Changed from Promise<any | null>
  try {
    return await httpUtils.request("/user/idCardVerification", "GET", params, false, false, false);
  } catch (e) {
    return null;
  }
}

/////yckj/pay/getRechargeParams
export async function getTeamRechargeParamsApi(params: any): Promise<PayParamsModel | null> { // Changed from Promise<any | null>
  try {
    return await httpUtils.request("/pay/getTeamRechargeParams", "POST", params, false, false, false);
  } catch (e) {
    return null;
  }
}

export async function getUserRechargeParamsApi(params: any): Promise<PayParamsModel | null> { // Changed from Promise<any | null>
  try {
    return await httpUtils.request("/pay/getUserRechargeParams", "POST", params, false, false, false);
  } catch (e) {
    return null;
  }
}

////yckj/pay/getPayParams


////yckj/user/available/balance
export async function availableBalanceApi(teamId?: any, isRefresh = false): Promise<BalanceModel | null> { // Changed from Promise<any | null>
  try {
    return await httpUtils.request("/user/available/balance", "GET", {
      teamId: teamId ? teamId : '-1'
    }, false, false, false, isRefresh)
  } catch (e) {
    return null;
  }
}

export async function userBalanceApi(teamId?: any, isRefresh = false): Promise<BalanceModel | null> { // Changed from Promise<any | null>
  try {
    return await httpUtils.request("/user/balance", "GET", {
      teamId: teamId ? teamId : '-1'
    }, false, false, true, isRefresh)
  } catch (e) {
    return null;
  }
}

export async function getUserInfoApi(isRefresh = false): Promise<UserinfoModel | null> {
  if (!LocalUtils.isLogin()) {
    return null
  }
  try {
    return await httpUtils.request("/user/info", "GET", {}, false, false, false, isRefresh);
  } catch (e) {
    return null;
  }
}

///yckj/order/balance/pay

export async function balancePayApi(orderId, payWay): Promise<any> {
  try {
    return await httpUtils.request("/order/balance/pay", "POST", {
      orderId: orderId,
      payWay: payWay,
    }, false, true, false);
  } catch (e) {
    return null;
  }
}

///yckj/pay/getPayParams
export async function getPayParamsApi(orderId): Promise<PayParamsModel | null> {
  try {
    return await httpUtils.request("/pay/getPayParams", "POST", {
      businessId: orderId,
      businessType: 2,
    }, false, false, false);
  } catch (e) {
    return null;
  }
}

export async function byMobileApi(phone): Promise<UserinfoModel | null> {
  try {
    return await httpUtils.request("/user/byMobile", "GET", {
      mobile: phone
    }, false, false, true);
  } catch (e) {
    return null;
  }
}

//http://118.193.103.197:8080/api/yckj/team/search/list?name=1
export async function teamSearchListApi(name): Promise<TeamModel[] | null> {
  try {
    return await httpUtils.request("/team/search/list", "GET", {
      name: name
    }, false, false, true);
  } catch (e) {
    return null;
  }
}

export async function addressListApi(isRefresh = false): Promise<AddressModel[] | null> {
  try {
    return await httpUtils.request("/address/list", "GET", {}, true, false, false);
  } catch (e) {
    return null;
  }
}

export async function addressDeleteApi(addressId: string): Promise<any> { // Changed from Promise<any | null>
  try {
    return await httpUtils.request("/address/delete", "GET", {
      id: addressId
    }, true, false, false);
  } catch (e) {
    return null;
  }
}

export async function addressSaveApi(params: Map<string, any>): Promise<any> {
  try {
    return await httpUtils.request("/address/save", "POST", params, true, false, false);
  } catch (e) {
    return null;
  }
}

export async function addressUpdateApi(params): Promise<any> {
  try {
    return await httpUtils.request("/address/update", "POST", params, true, false, false);
  } catch (e) {
    return null;
  }
}


export async function invoiceListApi(): Promise<InvoiceModel[] | null> {
  try {
    return await httpUtils.request("/invoice/list", "GET", {}, false, false, false);
  } catch (e) {
    return null;
  }
}

//api/yckj/invoice/logs/opened/page?page=1&limit=1&orderField=1&order=1

export async function openedInvoiceListApi(isRefresh = false, page = LocalData.startPage): Promise<RechargeRecordModel[] | null> {
  try {
    const result = await httpUtils.request(`/invoice/logs/opened/page`, "GET", {
      page: page,
      limit: 200
    }, false, false, false, isRefresh);
    return result.list;
  } catch (e) {
    return null;
  }
}


/////yckj/invoice/logs/apply
export async function invoiceApplyApi(params): Promise<any> {
  try {
    return await httpUtils.request("/invoice/logs/apply", "POST", params, false, false, false);
  } catch (e) {
    return null;
  }
}

//invoice/logs/page
export async function invoiceLogsApi(page: number = LocalData.startPage, isRefresh = false): Promise<RechargeRecordModel[] | null> {
  try {
    const res: PageListModel<RechargeRecordModel> = await httpUtils.request("/invoice/logs/page", "GET", {
      page: page,
      limit: 10
    }, false, true, false, isRefresh);
    return res.list;
  } catch (e) {
    return null;
  }
}

export async function cateListApi(): Promise<CategoryModel[] | null> {
  try {
    return await httpUtils.request("/cate/list", "GET", {}, false, false, false);
  } catch (e) {
    return null;
  }
}

export async function invoiceDeleteApi(addressId: string): Promise<any> { // Changed from Promise<any | null>
  try {
    return await httpUtils.request("/invoice/delete", "GET", {
      id: addressId
    }, false, false, false);
  } catch (e) {
    return null;
  }
}

export async function invoiceSaveApi(params: Map<string, any>): Promise<any> {
  try {
    return await httpUtils.request("/invoice/save", "POST", params, false, false, false);
  } catch (e) {
    return null;
  }
}

export async function invoiceUpdateApi(params): Promise<any> {
  try {
    return await httpUtils.request("/invoice/update", "POST", params, false, false, false);
  } catch (e) {
    return null;
  }
}

export async function adsListApi(): Promise<AdsModel[] | null> {
  try {
    return await httpUtils.request("/ads/list", "GET", {}, false, false, true);
  } catch (e) {
    return null;
  }
}

//团队
export async function teamListApi(): Promise<TeamModel[] | null> {
  try {
    return await httpUtils.request("/team/create/list", "GET", {}, false, false, false);
  } catch (e) {
    return null;
  }
}

export async function teamApplyListApi(): Promise<ApplyTeamModel[] | null> {
  try {
    return await httpUtils.request("/team/apply/list", "GET", {}, false, false, false);
  } catch (e) {
    return null;
  }
}

export async function teamPassApi(id): Promise<any> {
  try {
    return await httpUtils.request("/team/relation/pass", "POST", {
      id: id
    }, false, false, false);
  } catch (e) {
    return null;
  }
}

export async function teamJoinApi(hideLoading = false): Promise<TeamModel[] | null> {
  try {
    return await httpUtils.request("/team/join/list", "GET", {}, false, false, hideLoading);
  } catch (e) {
    return null;
  }
}

export async function teamDeleteApi(id): Promise<any> {
  try {
    return await httpUtils.request("/team/delete", "GET", {
      id: id
    }, false, false, false);
  } catch (e) {
    return null;
  }
}

////yckj/team/relation/list
export async function teamRelationListApi(id): Promise<RelationModel[] | null> {
  try {
    return await httpUtils.request("/team/relation/list", "GET", {
      teamId: id,
      status: 1,
    }, false, false, false);
  } catch (e) {
    return null;
  }
}

export async function teamRelationInfoApi(teamId): Promise<RelationModel | null> {
  try {
    return await httpUtils.request("/team/relation/info", "POST", {
      teamId: teamId
    }, false, true, false);
  } catch (e) {
    return null;
  }
}

export async function teamInfoApi(teamId): Promise<TeamModel | null> {
  try {
    return await httpUtils.request("/team/info", "GET", {
      id: teamId
    }, false, false, true);
  } catch (e) {
    return null;
  }
}

export async function teamRelationSaveApi(teamId, userId): Promise<any> {
  try {
    return await httpUtils.request("/team/relation/save", "POST", {
      teamId: teamId,
      userId: userId
    }, false, true, false);
  } catch (e) {
    return null;
  }
}

export async function teamSaveApi(params): Promise<any> {
  try {
    return await httpUtils.request("/team/save", "POST", params, false, false, false);
  } catch (e) {
    return null;
  }
}

///yckj/team/relation/delete
export async function relationDeleteApi(teamId, id): Promise<any> {
  try {
    return await httpUtils.request("/team/relation/delete", "GET", {
      teamId: teamId,
      id: id,
    }, false, false, false);
  } catch (e) {
    return null;
  }
}

///yckj/project/hot
export async function projectHotApi(): Promise<ProjectModel[] | null> {
  try {
    return await httpUtils.request("/project/hot", "GET", {}, false, false, true);
  } catch (e) {
    return null;
  }
}

export async function otherListApi(
  projectId
): Promise<MoreOptions[] | null> {
  try {
    return await httpUtils.request("/project/otherList", "GET", {
      projectId: projectId
    }, false, false, true);
  } catch (e) {
    return null;
  }
}

////yckj/machine/list
export async function machineListApi(projectId): Promise<MachineModel[] | null> {
  try {
    return await httpUtils.request("/machine/project/list", "GET", {
      projectId: projectId
    }, false, false, false);
  } catch (e) {
    return null;
  }
}

///yckj/machine/list
export async function machineList2Api(hideLoading = false): Promise<MachineModel[] | null> {
  try {
    return await httpUtils.request("/machine/list", "GET", {}, false, false, hideLoading);
  } catch (e) {
    return null;
  }
}

////yckj/common/uploadFile
export async function uploadFileApi(filePath, type = 'user'): Promise<FileModel | null> {
  try {
    return await httpUtils.uploadFile("/common/uploadFile", filePath, 'file', {
      type: type
    }, false);
  } catch (e) {
    return null;
  }
}

////yckj/project/machine/list
export async function projectMachineListApi(machineId, hideLoading = false, isRefresh = false): Promise<ProjectModel[] | null> {
  try {
    return await httpUtils.request("/project/machine/list", "GET", {
      machineId: machineId
    }, false, false, hideLoading, isRefresh);
  } catch (e) {
    return null;
  }
}


export async function orderSignApi(signImage, orderId): Promise<any> {
  try {
    return await httpUtils.request("/order/sign", "POST", {
      signImage: signImage,
      orderId: orderId,
    }, false, true, false);
  } catch (e) {
    return null;
  }
}


export async function orderSaveApi(params): Promise<any> {
  try {
    return await httpUtils.request("/order/save", "POST", params, false, false, false);
  } catch (e) {
    return null;
  }
}

export async function orderPayApi(params): Promise<any> {
  try {
    return await httpUtils.request("/order/save", "POST", params, false, false, false);
  } catch (e) {
    return null;
  }
}

///yckj/order/cancel
export async function orderCancelApi(id): Promise<any> {
  try {
    return await httpUtils.request("/order/cancel", "POST", {
      orderId: id
    }, false, true, false);
  } catch (e) {
    return null;
  }
}

export async function orderTimeApi(machineId): Promise<String[] | null> {
  try {
    return await httpUtils.request("/order/time", "GET", {
      machineId: machineId
    }, false, false, true);
  } catch (e) {
    return null;
  }
}

export async function orderListApi(status, isRefresh = false): Promise<OrderItem[] | null> {
  try {
    return await httpUtils.request("/order/list", "GET", {
      status: status,
    }, true, false, false, isRefresh);
  } catch (e) {
    return null;
  }
}

export async function orderListPageApi(status, isRefresh = false, page: number): Promise<OrderItem[] | null> {

  const params = {
    page: page,
    limit: LocalData.limit,
  }
  if (status !== -1) {
    params.status = status;
  }
  try {
    const result = await httpUtils.request("/order/page", "GET", params, true, false, false, isRefresh);
    return result.list;
  } catch (e) {
    return null;
  }
}


export async function universityListApi(name: string, isRefresh = false, page: number): Promise<UniversityModel[] | null> {
  const params = {
    page: page,
    limit: 100,
    key: name
  }
  try {
    const result = await httpUtils.request("/common/school/page", "GET", params, true, false, false, isRefresh);
    return result.list;
  } catch (e) {
    return null;
  }
}

export async function majorsListApi(name: string): Promise<UniversityModel[] | null> {
  const params = {
    schoolName: name
  }
  try {
    return await httpUtils.request("/common/majors/list", "GET", params, false, false, false);
  } catch (e) {
    return null;
  }
}

//api/yckj/invoice/logs/opened/page?page=1&limit=1&orderField=1&order=1

export async function orderInfoApi(id): Promise<OrderItem | null> {
  try {
    return await httpUtils.request("/order/info", "GET", {
      id: id,
    }, false, false, false);
  } catch (e) {
    return null;
  }
}


///yckj/order/team/view/list
export async function teamViewListApi(teamId, type, isRefresh): Promise<OrderItem[] | null> {
  try {
    return await httpUtils.request("/order/team/view/list", "GET", {
      teamId: teamId,
      type: type,
    }, false, false, false, isRefresh);
  } catch (e) {
    return null;
  }
}

///yckj/order/team/approve/list
export async function teamApproveListApi(teamId, isRefresh): Promise<OrderItem[] | null> {
  try {
    return await httpUtils.request("/order/team/approve/list", "GET", {
      teamId: teamId,
    }, false, false, false, isRefresh);
  } catch (e) {
    return null;
  }
}


export async function orderTeamApproveApi(teamId, orderId, status): Promise<any> {
  try {
    return await httpUtils.request("/order/team/approve", "POST", {
      teamId: teamId,
      orderId: orderId,
      status: status
    }, false, true, false);
  } catch (e) {
    return null;
  }
}

////yckj/team/relation/update

export async function relationUpdateApi(params): Promise<any> {
  try {
    return await httpUtils.request("/team/relation/update", "POST", params, false, false, false);
  } catch (e) {
    return null;
  }
}

////yckj/activity/list

export async function activityListApi(): Promise<ZiXunModel[] | null> {
  try {
    return await httpUtils.request("/activity/list", "GET", {}, false, false, false);
  } catch (e) {
    return null;
  }
}

export async function activityWordsApi(): Promise<HotWordModel[] | null> {
  try {
    return await httpUtils.request("/activity/words", "GET", {}, false, false, false);
  } catch (e) {
    return null;
  }
}

////yckj/project/search
export async function projectSearchApi(name): Promise<ProjectModel[] | null> {
  try {
    return await httpUtils.request("/project/search", "GET", {
      name: name
    }, false, false, false);
  } catch (e) {
    return null;
  }
}


///yckj/asset/recharge/list

export async function rechargeListApi(): Promise<RechargeModel[] | null> {
  try {
    return await httpUtils.request("/asset/recharge/list", "GET", {}, false, false, false);
  } catch (e) {
    return null;
  }
}

export async function assetListApi(): Promise<RechargeModel[] | null> {
  try {
    return await httpUtils.request("/asset/list", "GET", {}, false, false, false);
  } catch (e) {
    return null;
  }
}

///yckj/coupons/receive
export async function couponsReceiveApi(couponsId: number): Promise<any> {
  try {
    return await httpUtils.request("/coupons/receive", "GET", {
      couponsId: couponsId
    }, false, false, false);
  } catch (e) {
    return null;
  }
}

//可领取列表/yckj/coupons/expire/page
///yckj/coupons/available/page
export async function couponsListPageApi(status: number, hideLoading = false): Promise<CouponsModel[] | null> {
  let api: string;
  if (status === 0) {
    api = "/coupons/available/page";
  } else if (status === 1) {
    api = "/coupons/receive/page";
  } else if (status === 2) {
    api = "/coupons/used/page";
  } else {
    api = "/coupons/expire/page";
  }
  try {
    const result = await httpUtils.request(api, "GET", {}, false, false, false);
    return result.list;

  } catch (e) {
    return null;
  }
}



